////////////////////////////////////////////////////////////////////////////////
//
//  The MIT License
//
//  Copyright (c) 2008 - 2010. Dimarik
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////

package ru.goodcore.utils.swf
{

import flash.utils.ByteArray;
import flash.utils.Endian;

import ru.goodcore.utils.traceByteArray;

/**
 *  @author				Dimarik
 *  @version			1.1
 *  @langversion		3.0
 *  @playerversion		9.0
 */
public class SWFImage extends ByteArray {

	//-------------------------------------------------------------------------
	//
	//   Constructor
	//
	//-------------------------------------------------------------------------

	/**
	 * Constructor
	 * 
	 * @param input		source bitmap data
	 * @param qname		full qualified name
	 * 
	 */
	public function SWFImage(input:ByteArray, qname:QName, transparent:Boolean = false) {
		super();
		super.endian = Endian.LITTLE_ENDIAN;

		var ba:ByteArray = new ByteArray();
		ba.endian = Endian.LITTLE_ENDIAN;

		var data:Array = [
			0x09535746, 0x00000000, 0xA0000A30, 0x00010C00, 
			0x00081144, 0x02430000, 0xBFFFFFFF, 0x00000B15,
			0x53000100, 0x656E6563, 0x00003120
		];

		var len:uint = data.length;

		for (var i:uint=0; i<len; super.writeUnsignedInt(data[i++])) {}

		data = [
			0x00000000, 0x00100000, 0x10000000, 0x00002E00,
			0x00090000, 0x6D754E06
		];

		for (i=0, len=data.length; i<len; ba.writeUnsignedInt(data[i++])) {}

		ba.writeShort(0x6562); ba.writeByte(0x72);

		if (qname.uri) {
			ba.writeByte(qname.uri.length);
			ba.writeUTFBytes(qname.uri);
		}

		ba.writeByte(qname.localName.length);
		ba.writeUTFBytes(qname.localName);

		data = [
			0x616C660D, 0x642E6873, 0x6C707369, 0x420A7961,
			0x616D7469, 0x74614470
		];

		for (i=0, len=data.length; i<len; ba.writeUnsignedInt(data[i++])) {}

		ba.writeByte(0x61);

		len = qname.localName.length + 1;

		if (qname.uri)
			len += qname.uri.length;

		ba.writeByte(len);

		if (qname.uri)
			ba.writeUTFBytes(qname.uri + ':');

		ba.writeUTFBytes(qname.localName);

		data = [
			0x6A624F06, 0x05746365, 0x03160116, 0x07180516,
			0x01070500, 0x04020702, 0x07060307, 0x00030801,
			0x02000000, 0x00010100, 0x00000000, 0x02010000,
			0x00040803, 0x00000001, 0x02010201, 0x03000104,
			0x04010100, 0x30D00305, 0x01000047, 0x06050303,
			0xD030D008, 0x0249D2D1, 0x02000047, 0x04010102,
			0x6530D013, 0x30046000, 0x60300360, 0x1D005803,
			0x4702681D
		];

		for (i=0, len=data.length; i<len; ba.writeUnsignedInt(data[i++])) {}

		ba.writeShort(0x0000);

		this.writeTag(0x52, ba);
		super.writeBytes(ba);

		ba.length = 0;

		// RECORDHEADER (long)
		ba.writeByte(0x00); // 1
		ba.writeByte(0x00); // 2
		ba.writeByte(0x00); // 3
		ba.writeByte(0x00); // 4
		ba.writeByte(0x00); // 5
		ba.writeByte(0x00); // 6

		// CharacterID UI16
		ba.writeByte(0x01);
		ba.writeByte(0x00);

		//AlphaDataOffset UI32
		ba.writeUnsignedInt(input.length + 2);

		// DeblockParam UI16
		ba.writeByte(0x00);
		ba.writeByte(0x00);

		// ImageData UI8[data size]
		ba.writeBytes(input);
		this.writeTag(0x5A, ba);

		super.writeBytes(ba);

		ba.length = 0;
		ba.writeUnsignedInt(0x00000000); ba.writeUnsignedInt(0x00010000);
		ba.writeShort(0x0001);

		if (qname.uri)
			ba.writeUTFBytes(qname.uri + '.');

		ba.writeUTFBytes(qname.localName); ba.writeByte(0x00);

		this.writeTag(0x4C, ba);

		super.writeBytes(ba);
		super.writeUnsignedInt(0x000040);

		super.position = 4;
		super.writeUnsignedInt(super.length);
		super.position = 0;
	}

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	/**
	 * @private
	 * @param id	tag id
	 * @param ba	bytearray
	 */	
	private function writeTag(id:uint, ba:ByteArray):void {
		ba.position = 0;
		ba.writeShort(id << 6 | 0x3F);
		ba.writeUnsignedInt(ba.length - 6);
	}

}
}

